//
//  MultipleBarChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/6.
//  Copyright © 2016年 apple. All rights reserved.
//
#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "MultipleBarChartViewController.h"
@interface MultipleBarChartViewController ()
@end

@implementation MultipleBarChartViewController

BarChartView *sampleMultipleBarChartView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initBarChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initBarChartView{
    sampleMultipleBarChartView=[[BarChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleMultipleBarChartView.backgroundColor = [UIColor whiteColor];
    sampleMultipleBarChartView.delegate = self;
    [self.view addSubview:sampleMultipleBarChartView];
    [self initBarChartStyleAndData];
}

-(void)initBarChartStyleAndData{
    [self setupBarChartView:sampleMultipleBarChartView];
    [self setBarChartParam:sampleMultipleBarChartView];
    [self setLegendStyle:sampleMultipleBarChartView];
    [self initChartData];
}

- (void)setupBarChartView:(BarChartView *)chartView
{
    //描述
    chartView.descriptionText = @"descriptionText";
    //无数据默认显示
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    //网格背景
    chartView.drawGridBackgroundEnabled = NO;
    chartView.drawBarShadowEnabled = NO;
    chartView.dragEnabled = YES;
    [chartView setScaleEnabled:YES];
    chartView.pinchZoomEnabled = NO;
}

-(void)setBarChartParam:(BarChartView *)chartView{
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    
    ChartYAxis *leftAxis = chartView.leftAxis;
    leftAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];
    leftAxis.valueFormatter.maximumFractionDigits = 1;
    leftAxis.drawGridLinesEnabled = NO;
    leftAxis.spaceTop = 0.25;
    
    chartView.rightAxis.enabled = NO;
}

//设置图例样式
-(void)setLegendStyle:(BarChartView *)chartView{
    //图例在柱状图中的位置
    chartView.legend.position = ChartLegendPositionRightOfChartInside;
    //字体大小
    chartView.legend.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
}

- (void)initChartData
{
    [self setDataCount:5 range:100 chart:sampleMultipleBarChartView];
}

- (void)setDataCount:(int)count range:(double)range chart:(BarChartView *)chartView
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++){
        [xVals addObject:[@(i + 1990) stringValue]];
    }
    
    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals2 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals3 = [[NSMutableArray alloc] init];
    
    double mult = range * 1000.f;
    
    for (int i = 0; i < count; i++){
        double val = (double) (arc4random_uniform(mult) + 3.0);
        [yVals1 addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:i]];
        
        val = (double) (arc4random_uniform(mult) + 3.0);
        [yVals2 addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:i]];
        
        val = (double) (arc4random_uniform(mult) + 3.0);
        [yVals3 addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    
    BarChartDataSet *set1 = nil, *set2 = nil, *set3 = nil;
    if (chartView.data.dataSetCount > 0){
        set1 = (BarChartDataSet *)chartView.data.dataSets[0];
        set2 = (BarChartDataSet *)chartView.data.dataSets[1];
        set3 = (BarChartDataSet *)chartView.data.dataSets[2];
        set1.yVals = yVals1;
        set2.yVals = yVals2;
        set3.yVals = yVals3;
        chartView.data.xValsObjc = xVals;
        [chartView notifyDataSetChanged];
    }else{
        set1 = [[BarChartDataSet alloc] initWithYVals:yVals1 label:@"Company A"];
        [set1 setColor:[UIColor colorWithRed:104/255.f green:241/255.f blue:175/255.f alpha:1.f]];
        
        set2 = [[BarChartDataSet alloc] initWithYVals:yVals2 label:@"Company B"];
        [set2 setColor:[UIColor colorWithRed:164/255.f green:228/255.f blue:251/255.f alpha:1.f]];
        
        set3 = [[BarChartDataSet alloc] initWithYVals:yVals3 label:@"Company C"];
        [set3 setColor:[UIColor colorWithRed:242/255.f green:247/255.f blue:158/255.f alpha:1.f]];
        
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        [dataSets addObject:set2];
        [dataSets addObject:set3];
        
        BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
        data.groupSpace = 0.8;
        [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];

        chartView.data = data;
    }
}


#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end